package com.entrogy.controller;

import com.entrogy.service.KaoqinService;
import com.entrogy.service.UserService;
import com.entrogy.utils.ApiReturnUtil;
import com.entrogy.utils.StrUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @author xlr
 * @date 2019-7
 * @description 学生考勤模块
 */
@Api(description = "学生考勤信息的相关接口（考勤查询，考勤流水，返校统计）")
@RestController
@RequestMapping(value = "kaoqin")
public class StudentKaoqinController {

    private static final Logger logger = LoggerFactory.getLogger(StudentKaoqinController.class);

    @Autowired
    private KaoqinService kaoqinService;

    @Autowired
    private UserService userService;


    @ApiOperation(value = "PC端考勤查询的初始化")
    @ApiImplicitParam(value = "用户ID",name = "uid",paramType = "query",dataType = "String",required = true)
    @RequestMapping(value = "/kaoqininit",method = RequestMethod.GET)
    public JSONObject kaoqinInit(String uid) {
        JSONObject jsonObject = kaoqinService.kaoqinInit(uid);
        if (jsonObject == null) {
            jsonObject.put("code", "0");
            jsonObject.put("msg", "没有权限");
            return jsonObject;
        }
        jsonObject.put("code", "1");
        jsonObject.put("msg", "ok");
        return jsonObject;
    }

    @ApiOperation(value = "PC端考勤查询功能，只用返回list3")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "查询的范围",name = "select1id",paramType = "query",dataType = "String",required = true),
            @ApiImplicitParam(value = "用户ID",name = "uid",paramType = "query",dataType = "String",required = true),
            @ApiImplicitParam(value = "选择的时间段",name = "timeid",paramType = "query",dataType = "String",required = true),
            @ApiImplicitParam(value = "选择的日期",name = "date",paramType = "query",dataType = "String",required = true)
    })
    @RequestMapping(value = "getkaoqin",method = RequestMethod.GET)
    public Map<String,Object> getKaoqin(String select1id, String uid, String timeid, String date){
       JSONArray jsonArray = kaoqinService.getKaoqin(uid, select1id, timeid, date);
        return ApiReturnUtil.success("查询成功",jsonArray);
    }


    /**
     * 看要查询的学生是否在这个老师的权限范围内
     * ->如果在权限范围内，返回指定时间范围内的流水数据（如果时间为空，则返回全部流水记录）
     * ->如果不在权限范围内，返回错误信息
     */
    @ApiOperation(value = "考勤流水模块（看这个老师有没有查询该学生的权限，如果有查询指定时间内的流水数据）")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "开始时间",name = "startTime",paramType = "query",dataType = "String",required = true),
            @ApiImplicitParam(value = "结束时间",name = "endTime",paramType = "query",dataType = "String",required = true),
            @ApiImplicitParam(value = "姓名",name = "name",paramType = "query",dataType = "String",required = true),
            @ApiImplicitParam(value = "用户ID",name = "uid",paramType = "query",dataType = "String",required = true),
            @ApiImplicitParam(value = "页码",name = "page",paramType = "query",dataType = "String",required = false)
    })
    @RequestMapping(value = "/getrecord",method = RequestMethod.GET)
    public Map<String,Object> getStudentRecord(String startTime, String endTime, String name, String uid, String page){
        int pageInt = 0;
        if (!StrUtils.isNullOrEmpty(page)) {
            pageInt = Integer.parseInt(page);
        }
        // 要查询的对象是否在权限范围内
        JSONObject jsonObject = null;
        try {
            Integer schoolCode = userService.getSchoolCodeByUserId(uid);
            jsonObject = kaoqinService.getStudentRecord(name, uid, startTime, endTime, pageInt, schoolCode);
            jsonObject.put("code", "1");
            jsonObject.put("msg", "ok");
        } catch (Exception e) {
            jsonObject.put("code", "1");
            jsonObject.put("msg", e.toString());
        }
        return jsonObject;
    }
    /**
     * 返校日考勤
     * 如果当天不是返校日,则提示当天不是返校日
     * 如果当天是返校日，则根据年级返回
     */
    @ApiOperation(value = "返校日考勤的初始化，如果当天不是返校日,则提示当天不是返校日， 如果当天是返校日，则根据年级返回")
    @ApiImplicitParam(value = "用户ID",name = "uid",paramType = "query",dataType = "String",required = true)
    @RequestMapping(value = "/getbacktoschoolinit",method = RequestMethod.GET)
    public JSONObject getBacktoSchoolInit(String uid){
        JSONObject jsonObject = new JSONObject();
       /* if (StuRecordService.dayType == 1) { -----废弃了
            try {
                jsonObject = kaoqinService.backtoSchoolInit(uid);
                jsonObject.put("code", "1");
                jsonObject.put("msg", "ok");
            } catch (Exception e) {
                e.printStackTrace();
                jsonObject.put("code", "0");
                jsonObject.put("msg", "查询失败");
            }
        } else {
            jsonObject.put("code", "1");
            jsonObject.put("msg", "今天不是返校日");
        }*/
        return jsonObject;
    }

    // 返校日筛选
    @ApiOperation(value = "返校日的查询")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "用户ID",name = "uid",paramType = "query",dataType = "String",required = true),
            @ApiImplicitParam(value = "班级ID",name = "courseId",paramType = "query",dataType = "String",required = true)
    })
    public JSONObject getBacktoSchoolSelect(String uid,String courseId) {
        JSONObject jsonObject = new JSONObject();
        JSONArray jsonArray = new JSONArray();
        try {
            jsonArray = kaoqinService.backtoSchoolSelect(uid,courseId);
        } catch (Exception e) {
            logger.error("查询返校统计失败");
            jsonObject.put("code", "0");
            jsonObject.put("msg", "查询失败");
        }
        jsonObject.put("code", "1");
        jsonObject.put("msg", "ok");
        jsonObject.put("list2", jsonArray);
        return jsonObject;
    }
}
